package com.david.videoapp.activity

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.Settings
import android.text.TextUtils
import android.view.View
import android.widget.VideoView
import com.david.core.event.MyEventBus
import com.david.core.event.Subscribe
import com.david.core.event.ThreadMode
import com.david.core.utils.PreferenceUtils
import com.david.videoapp.ChannelBean
import com.david.videoapp.event.OnChannelsRefresh
import com.david.videoapp.utils.LiveDataHelper
import java.util.*

class NormalVideoActivity:BaseVideoActivity() {
    private lateinit var videoView:VideoView
    override fun getVideoView(): View {
        if (!this::videoView.isInitialized) {
            videoView = VideoView(this)
        }
        return videoView
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        LiveDataHelper.getDataInstance().refreshData()

        MyEventBus.getDefault().register(this, "onChannelsRefresh", OnChannelsRefresh::class.java)

        //android11管理所以文件：https://developer.android.google.cn/training/data-storage/manage-all-files?hl=zh-cn, https://juejin.cn/post/7012262477828194340
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            //检查是否已经有权限
            if (!Environment.isExternalStorageManager()) {
                //跳转新页面申请权限
                startActivityForResult( Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION), 101)
            }
        }
    }

    override fun getChannels(): ArrayList<ChannelBean> {
        return LiveDataHelper.getDataInstance().channels
    }

    override fun setVideoPath(videoPath: String) {
        videoView.setVideoPath(videoPath)
    }

    override fun playVideo() {
        videoView.start()
    }

    override fun pauseVideo() {
        videoView.pause()
    }

    override fun stopPlay() {
        videoView.stopPlayback()
    }

    override fun isPlaying(): Boolean {
        return videoView.isPlaying
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onChannelsRefresh(onChannelsRefresh: OnChannelsRefresh){
        val currentTitle = PreferenceUtils.getInstance().getString("lastPlayTitle", "")
        channelAdapter.currentPosition = 0
        var changed = true
        if (!TextUtils.isEmpty(currentTitle)) {
            for (index in LiveDataHelper.getDataInstance().channels.indices) {
                if (currentTitle?.equals(LiveDataHelper.getDataInstance().channels[index].title) == true) {
                    channelAdapter.currentPosition = index
                    changed = false
                    break
                }
            }
        }
        channelAdapter.setChannels(LiveDataHelper.getDataInstance().channels)
        if (changed) {
            stopPlay()
            currentUrl = LiveDataHelper.getDataInstance().channels[channelAdapter.currentPosition].url
            setVideoPath(currentUrl)
            playVideo()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        MyEventBus.getDefault().unregister(this)

    }
}